﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.ComponentModel;
using System.Drawing;
using BrainTechLLC;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using System.Threading;
using BrainTechLLC.FilterExtensions;
using BrainTechLLC.ImageOperators;
using System.Windows.Media;

namespace ImageAppDataModels
{
    /// <summary>
    /// Stores a bitmap and a description of the bitmap
    /// </summary>
    public partial class BitmapAndDescription : BaseNotifyPropertyChanged, INotifyPropertyChanged
    {
        protected Bitmap _bitmap;
        protected Bitmap _originalBitmap;
        public int _width;
        public int _height;
        protected string _description;
        protected string _fileName;
        protected ISingleFilter _filter;
        protected bool _preventReentrancy;
        
        public ISingleFilter Filter
        {
            get { return _filter; }
            set
            {
                if (_filter != value)
                {
                    _filter = value;
                    _bitmap = null;
                    OnPropertyChanged("Filter");
                    OnPropertyChanged("Bitmap");
                }
            }
        }

        public Bitmap OriginalBitmap
        {
            get { return _originalBitmap; }
            set
            {
                if (_originalBitmap != value)
                {
                    _originalBitmap = value;
                    _bitmap = null;
                    OnPropertyChanged("OriginalBitmap");
                    OnPropertyChanged("Bitmap");
                }
            }
        }

        public string Description
        {
            get { return _description; }
            set
            {
                if (_description != value)
                {
                    _description = value;
                    OnPropertyChanged("Description");
                }
            }
        }

        public string FileName
        {
            get { return _fileName; }
            set
            {
                if (_fileName != value)
                {
                    _fileName = value;
                    OnPropertyChanged("FileName");
                }
            }
        }

        public Bitmap Bitmap
        {
            get
            {
                if (_preventReentrancy)
                {
                    return _bitmap;
                }

                _preventReentrancy = true;

                try
                {
                    if (_bitmap == null && _originalBitmap != null)
                    {
                        if (_originalBitmap != null)
                        {
                            if (_filter == null || _filter.Disabled)
                            {
                                _bitmap = new Bitmap(_originalBitmap);
                            }
                            else
                            {
                                try
                                {
                                    _bitmap = _filter.Apply("Unknown", new Bitmap(_originalBitmap), false);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.ToString());
                                }
                            }
                        }
                    }
                }
                finally
                {
                    _preventReentrancy = false;
                }

                return _bitmap;
            }
            set
            {
                if (_bitmap != value)
                {
                    _bitmap = value;

                    if (_bitmap != null)
                    {
                        _width = _bitmap.Width;
                        _height = _bitmap.Height;
                    }

                    OnPropertyChanged("Bitmap");
                }
            }
        }

        public ImageSource CreateImageSourceFromBitmap(byte[] bytes)
        {
            return Bitmap.CreateImageSourceFromBitmap(bytes);
        }
    }
}
